package scau.hotel.hotel_service.service.impl;

import scau.hotel.hotel_service.entity.Room;
import scau.hotel.hotel_model.controller.RespBean;
import scau.hotel.hotel_model.controller.RespBeanEnum;
import scau.hotel.hotel_service.vo.RoomVo;
import scau.hotel.hotel_service.mapper.RoomMapper;
import scau.hotel.hotel_service.service.RoomService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (Room)表服务实现类
 *
 * @author makejava
 * @since 2022-03-03 23:04:33
 */
@Service("roomService")
public class RoomServiceImpl implements RoomService {
    @Resource
    private RoomMapper roomMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param roomId 主键
     * @return 实例对象
     */
    @Override
    public Room queryById(Integer roomId) {
        return this.roomMapper.queryById(roomId);
    }

    /**
     * 分页查询
     *
     * @param room 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Room> queryAll(Room room) {
        return this.roomMapper.queryAll(room);
    }

    /**
     * 分页查询
     *
     * @param room        筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Room> queryByPage(Room room, PageRequest pageRequest) {
        long total = this.roomMapper.count(room);
        return new PageImpl<>(this.roomMapper.queryAllByLimit(room, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param roomVo 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean insert(RoomVo roomVo) {
        if(null == this.roomMapper.queryRoomByRoomNo(roomVo.getRoomNo())) {
            this.roomMapper.insert(roomVo);
            return RespBean.success();
        }else{
            return RespBean.error(RespBeanEnum.ROOM_NO_EXIST_ERROR);
        }

    }



    /**
     * 修改数据
     *
     * @param roomVo 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean update(RoomVo roomVo) {
        if (null == this.roomMapper.queryRoomByRoomNo(roomVo.getRoomNo())) {
            this.roomMapper.update(roomVo);
            return RespBean.success();
        }
        if(Objects.equals(roomVo.getRoomNo(), this.roomMapper.queryById(roomVo.getRoomId()).getRoomNo())){
            this.roomMapper.update(roomVo);
            return RespBean.success();
        }
        else {
            return RespBean.error(RespBeanEnum.ROOM_NO_EXIST_ERROR);
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param roomId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roomId) {
        return this.roomMapper.deleteById(roomId) > 0;
    }

    /**
     * 批量删除数据
     *
     * @param arrays 被删除数据传值对象
     * @return 是否成功
     */
    public boolean deleteMoreRoomById(List<String> arrays ) {
        if (arrays.isEmpty()) {
            return false;
        } else {
            for (int i = 0; i < arrays.size(); i++) {
                Integer roomId = Integer.valueOf(arrays.get(i));
                this.roomMapper.deleteById(roomId);
            }
            return true;
        }
    }

    /**
     * 通过roomNo查询单条数据
     *
     * @param roomNo 主键
     * @return 实例对象
     */
    @Override
    public Room queryRoomByRoomNo(String roomNo) {
        return this.roomMapper.queryRoomByRoomNo(roomNo);
    }
}
